<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          row-key="orderByNumber"
          :cell-style="cellStyle"
          :summary-method="getSummaries"
          :show-summary="true"
          :cell-class-name="cellClassName"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column
            v-for="(column,index) in tableColumns"
            :key="index"
            :prop="column.prop"
            :is-show="column.isShow"
            :label="column.label"
            :sortable="column.sortable"
            :width="column.width"
            :min-width="column.minWidth"
            :fixed="column.fixed"
            :align="column.align"
          />
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, exportList } from '@/api/saleManage/salesShipmentDetails'
import { getInfo as showDetailSaleInfo } from '@/api/saleManage/saleOrder'
import { getInfo } from '@/api/saleManage/saleSend'
import ShowDetail from '../../saleSend/showDetail.vue' // 查看页面
import showDetailSale from '@/views/saleManage/saleOrder/showDetail.vue' // 查看页面
import { getDictSelects } from '@/api/common/common'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '销售发货明细', // 页面唯一标识
      pageExportId: '销售发货明细导出',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        deliverTime: [],
        isAsc: 'desc',
        orderByColumn: 'deliverTime'
      },
      clearable: false, // 日期选择器不可清除
      totalWord: ['orderNumber', 'deliverNumber', 'cost'], // 合计导出字段
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 导出表格数据
      tableData2: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'deliverStatusName', label: '状态', canOrder: false, minWidth: '130', isRegular: true },
        { prop: 'deliverCode', label: '发货单号', minWidth: '150', isRegular: true },
        { prop: 'deliverByName', label: '制单人员', canOrder: false, minWidth: '140', isRegular: true },
        { prop: 'deliverTime', label: '制单日期', isTime: true, minWidth: '140', isRegular: true },
        { prop: 'saleCustomerName', label: '客户名称', canOrder: false, minWidth: '140', isRegular: true },
        { prop: 'freightByLabel', label: '运费承担方', canOrder: false, minWidth: '80', isRegular: true },
        { prop: 'costThousandths', label: '费用', align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length },
        { prop: 'deliverRemark', label: '单据备注', canOrder: false, position: 'left', minWidth: '200' },
        { prop: 'saleOrderCode', label: '销售订单号', canOrder: false, minWidth: '150' },
        { prop: 'saleCustomerDeliveryTime', label: '客户交期', canOrder: false, minWidth: '140' },
        { prop: 'materialCode', label: '物料编码', canOrder: false, position: 'left', minWidth: '130', isShow: false },
        { prop: 'materialName', label: '物料名称', canOrder: false, position: 'left', minWidth: '100' },
        { prop: 'materialModel', label: '规格型号', canOrder: false, position: 'left', minWidth: '130', isShow: false },
        { prop: 'materialUnitLabel', label: '物料单位', canOrder: false, minWidth: '80', isShow: false },
        { prop: 'orderNumber', label: '订单数量', canOrder: false, align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length },
        { prop: 'deliverNumber', label: '发货数量', canOrder: false, align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length }
        // { prop: 'remark', label: '备注', canOrder: false }
      ],
      exportTableColumns: [
        { prop: 'deliverStatusName', label: '状态', canOrder: false, minWidth: '80' },
        { prop: 'deliverCode', label: '发货单号', minWidth: '150' },
        { prop: 'deliverByName', label: '制单人员', canOrder: false, minWidth: '80' },
        { prop: 'deliverTime', label: '制单日期', isTime: true, minWidth: '140' },
        { prop: 'saleCustomerName', label: '客户名称', canOrder: false, minWidth: '130' },
        { prop: 'freightByLabel', label: '运费承担方', canOrder: false, minWidth: '80' },
        { prop: 'cost', label: '费用', align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length },
        { prop: 'deliverRemark', label: '单据备注', canOrder: false, position: 'left', minWidth: '200' },
        { prop: 'saleOrderCode', label: '销售订单号', canOrder: false, minWidth: '150' },
        { prop: 'saleCustomerDeliveryTime', label: '客户交期', canOrder: false, minWidth: '140' },
        { prop: 'materialCode', label: '物料编码', canOrder: false, position: 'left', minWidth: '130', isShow: false },
        { prop: 'materialName', label: '物料名称', canOrder: false, position: 'left', minWidth: '100' },
        { prop: 'materialModel', label: '规格型号', canOrder: false, position: 'left', minWidth: '130', isShow: false },
        { prop: 'materialUnitLabel', label: '物料单位', canOrder: false, minWidth: '80' },
        { prop: 'orderNumber', label: '订单数量', canOrder: false, align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length },
        { prop: 'deliverNumber', label: '发货数量', canOrder: false, align: 'right', position: 'right', minWidth: this.SETTINGS.settings_decimalDigits_length }
      ],
      defaultExport: [],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'deliverStatus', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' },
        { prop: 'deliverCode', label: '发货单号', type: 'input' },
        { prop: 'deliverByName', label: '制单人员', type: 'input' },
        { prop: 'deliverTime', label: '制单日期', type: 'time', isTime: true, defaultKey: 'deliverTimeDefaul', clearable: false, hasDefault: true },
        { prop: 'saleCustomerName', label: '客户名称', type: 'input' },
        { prop: 'freightBy', label: '运费承担方', type: 'select', optionsName: 'freightByOptions' },
        { prop: 'saleOrderCode', label: '销售订单号', type: 'input' },
        // { prop: 'saleCustomerDeliveryTime', label: '客户交期', type: 'time', isTime: true },
        { prop: 'materialCode', label: '物料编码', type: 'input' },
        { prop: 'materialName', label: '物料名称', type: 'input' },
        { prop: 'materialModel', label: '规格型号', type: 'input', isShow: false },
        { prop: 'materialUnitCode', label: '物料单位', type: 'dict', optionsName: 'statusOptions3', isShow: false }
      ],
      deliverTimeDefaul: [], // 存放字段的默认值

      operationData: [ // 右上角的操作按钮
        { label: '批量导出3', perm: '' },
        { label: '页面配置2', perm: '' }
      ],
      freightByOptions: [],
      equipmentList: [],
      statusOptions: [
        { value: '1', label: '待发货' },
        { value: '2', label: '已发货' }
      ],
      statusOptions2: [
        { value: undefined, label: '全部' },
        { value: '0', label: '未发货' },
        { value: '1', label: '部分发货' },
        { value: '2', label: '全部发货' }
      ],
      statusOptions3: [],
      equipmentList1: [],
      Updata: [],
      showKey: [
        {
          name: '基本信息',
          columns: [
            { type: 'form', name: '发货单号', key: 'code' },
            { type: 'form', name: '制单人员', key: 'deliverName' },
            { type: 'form', name: '制单日期', key: 'deliverTime' },
            // { type: 'form', name: '销售订单号', key: 'saleOrderCode' },
            { type: 'form', name: '客户名称', key: 'customerName' },
            { type: 'form', name: '交货方式', key: 'deliveryTypeLabel' },
            { type: 'form', name: '收货人', key: 'customerName' },
            { type: 'form', name: '运费承担方', key: 'freightByLabel' },
            { type: 'form', name: '费用', key: 'cost' },
            { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
            { type: 'form', name: '备注', key: 'remark', length: 'double' },
            {
              type: 'table', name: '', key: 'data'
            }
          ]
        },
        {
          name: '客户信息',
          columns: [
            { type: 'formData2', name: '交货方式', key: 'deliveryTypeLabel' },
            { type: 'formData2', name: '联系人', key: 'consignee' },
            { type: 'formData2', name: '联系方式', key: 'phone' },
            { type: 'formData2', name: '注意事项', key: 'attentionMatters' },
            { type: 'formData2', name: '收货地址', key: 'deliveryAddress', length: 'double' }
          ]
        }
      ],
      showKey2: [
        {
          name: '基本信息',
          columns: [
            // { type: 'form', name: '销售订单号', key: 'code' },
            { type: 'form', name: '制单人员', key: 'orderName' },
            { type: 'form', name: '制单日期', key: 'orderTime' },
            { type: 'form', name: '订单类型', key: 'orderTypeLabel' },
            { type: 'form', name: '客户名称', key: 'customerName' },
            { type: 'form', name: '订货日期', key: 'orderGoodsTime' },
            { type: 'form', name: '合同号', key: 'contractCode' },
            { type: 'form', name: '未税总金额', key: 'allPrice' },
            { type: 'form', name: '含税总金额', key: 'taxAllPrice' },
            { type: 'form', name: '税额', key: 'tax' },
            { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'url' },
            { type: 'form', name: '备注', key: 'remark', length: 'double' },
            {
              type: 'table', name: '', key: 'data'
            }
          ]
        },
        {
          name: '客户信息',
          columns: [
            { type: 'formData', name: '计税方式', key: 'taxTypeLabel' },
            { type: 'formData', name: '税率%', key: 'taxRateLabel' },
            { type: 'formData', name: '结算方式', key: 'settlementTypeLabel' },
            { type: 'formData', name: '结算币别', key: 'settlementCurrencyLabel' },
            { type: 'formData2', name: '交货方式', key: 'deliveryTypeLabel' },
            { type: 'formData2', name: '联系人', key: 'consignee' },
            { type: 'formData2', name: '联系方式', key: 'phone' },
            { type: 'formData2', name: '注意事项', key: 'attentionMatters' },
            { type: 'formData2', name: '收货地址', key: 'deliveryAddress', length: 'double' }
          ]
        },
        {
          name: '审批详情',
          columns: [
            { type: 'review', name: '评审进度', key: 'taskList' }
          ]
        }
      ]
    }
  },
  watch: {
    tableData() {
      this.$nextTick(() => {
        for (const elementNodeListOfElement of document.querySelectorAll('.SaleOrderCodeColumnClass')) {
          if (!elementNodeListOfElement.childNodes[0].childNodes[0].className || elementNodeListOfElement.childNodes[0].childNodes[0].className == 'commonTableColumn') {
            const paddingNode = document.createElement('div')
            paddingNode.className = 'SaleOrderCodeColumnClass_expand'
            paddingNode.innerHTML = ''
            elementNodeListOfElement.childNodes[0].insertBefore(paddingNode, elementNodeListOfElement.childNodes[0].childNodes[0])
          }
        }
      })
    }
  },
  created() {
    // 获取当前日期
    this.deliverTimeDefaul = [this.$moment().startOf('month').format('YYYY-MM-DD') + ' 00:00',
      this.$moment(new Date()).format('YYYY-MM-DD') + ' 23:59']
  },
  mounted() {
    this.getDictSelects()
  },
  methods: {
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (columnIndex == 1) {
        return 'SaleOrderCodeColumnClass'
      }
      if (!row.isNotselected) {
        return 'displayNoneClass'
      }
    },
    cellStyle({ row, column }) {
      if (column.property == 'deliverCode') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
      // if (column.property == 'saleOrderCode') {
      //   return {
      //     cursor: 'pointer',
      //     color: '#387bfc'
      //   }
      // }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'deliverCode':
          this.showDetail(row)
          break
        // case 'saleOrderCode':
        //   this.showDetailSale(row)
        //   break
      }
    },
    showDetailSale(row) {
      showDetailSaleInfo(row.saleOrderId).then(res => {
        res.data.status = 3
        this.$layer.iframe({// 弹框
          content: {
            content: showDetailSale, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey2
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看销售订单号', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    showDetail(row) {
      getInfo(row.deliverId).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: ShowDetail, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              businessId: row.id,
              isNotShow: true,
              showData: {
                ...res.data,
                customerName: res.data.saleCustomerListVo.name,
                deliveryTypeLabel: res.data.saleDeliverInformation ? res.data.saleDeliverInformation.deliveryTypeLabel : ''
              },
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看销售发货单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
    },
    getList() {
      list(this.pageData).then(res => {
        res.data.dataTable.data.forEach((item) => { item.isNotselected = true })
        this.tableData = res.data.dataTable.data
        console.log(this.tableData)
        this.total = res.data.dataTable.total
        this.Updata = res.data
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
          this.$refs.table.doLayout()
        })
      })
    },
    // 保养级别字典
    getDictSelects() {
      getDictSelects('materialUnitId').then(res => {
        this.statusOptions3 = res.data
        res.data.unshift({ dictCode: undefined, dictLabel: '全部' },)
      })
      getDictSelects('freightBy').then(res => {
        res.data.forEach(ele => {
          ele['label'] = ele.dictLabel
          ele['value'] = ele.dictCode
        })
        this.freightByOptions = res.data
      })
    },
    // 导出
    getExportData(data) {
      return new Promise(function(resolve, reject) {
        exportList(data).then(res => {
          resolve({ data: res.data.dataTable })
        //   this.tableData = res.data.dataTable.data
        })
      })
    },
    // 合计行
    getSummaries(param) {
      const {
        columns
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index == 0) {
          sums[index] = '合计'
          return
        }
        switch (column.property) {
          case 'orderNumber':
            sums[index] = this.Updata.orderNumberSum
            break
        }
        switch (column.property) {
          case 'deliverNumber':
            sums[index] = this.Updata.deliverNumberSum
            break
        }
        switch (column.property) {
          case 'cost':
            sums[index] = this.Updata.costSum
            break
        }
      })
      return sums
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-tooltip{
  display: flex;
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: rgba(0,0,0,0);
}
/deep/ .el-table__footer-wrapper tbody td.el-table__cell{
  background-color: transparent!important;
}
/deep/ .el-table th.el-table__cell.is-leaf, .el-table td.el-table__cell {
  border-bottom: 1px solid #dfe6ec;
}

/deep/.el-table th.el-table__cell>.cell{
  padding-right: 15%;
}
/deep/.el-table .cell{
  padding-right: 15%;
}
/deep/ .SaleOrderCodeColumnClass_expand{
  display: inline-block;
  width: 20px;
  line-height: 20px;
  height: 20px;
  text-align: center;
  margin-right: 3px;
}
/deep/.el-table .displayNoneClass .cell .el-checkbox__inner{
  display: none
}
</style>
